package log

import (
	"io"
	"os"

	"github.com/rs/zerolog"
)

type ZeroLogger struct {
	zerolog.Logger
	skip int
}

func (logger *ZeroLogger) Debug(format string, args ...any) {
	logger.Logger.Debug().CallerSkipFrame(logger.skip).Msgf(format, args...)
}

func (logger *ZeroLogger) Info(format string, args ...any) {
	logger.Logger.Info().CallerSkipFrame(logger.skip).Msgf(format, args...)
}

func (logger *ZeroLogger) Warn(format string, args ...any) {
	logger.Logger.Warn().CallerSkipFrame(logger.skip).Msgf(format, args...)
}

func (logger *ZeroLogger) Error(format string, args ...any) {
	logger.Logger.Error().CallerSkipFrame(logger.skip).Msgf(format, args...)
}

func NewZeroLogger(writer io.Writer) *ZeroLogger {
	if writer == nil {
		return nil
	}

	if writer == os.Stdout {
		writer = zerolog.NewConsoleWriter(func(w *zerolog.ConsoleWriter) {
			w.Out = writer
			w.TimeFormat = timeFormat
		})
	}

	zeroLogger := &ZeroLogger{
		Logger: zerolog.New(writer).With().Timestamp().Caller().Logger(),
		skip:   2,
	}

	zerolog.TimeFieldFormat = timeFormat

	return zeroLogger
}
